/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
import { jobGet } from "@/api/pro";
import { RootState } from "@/store";
import { JobType } from "@/types/pro";
import { EllipsisOutlined, PlusOutlined } from "@ant-design/icons";
import type { ActionType, ProColumns } from "@ant-design/pro-components";
import { ProTable, TableDropdown } from "@ant-design/pro-components";
import { Button, Dropdown, Space, Tag } from "antd";
import { useRef } from "react";
import { useSelector } from "react-redux";
// import request from 'umi-request';
export const waitTimePromise = async (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export const waitTime = async (time: number = 100) => {
  await waitTimePromise(time);
};

const columns: ProColumns<JobType>[] = [
  {
    dataIndex: "index",
    valueType: "indexBorder",
    width: 48,
  },
  {
    dataIndex: "jobName",
    title: "岗位名称",
  },
  {
    dataIndex: "salaryDesc",
    title: "薪资范围",
    valueType: "select",
    valueEnum: {
      lv1: { text: "5~10K" },
      lv2: { text: "10~15K" },
      lv3: { text: "15~20K" },
      lv4: { text: "20以上" },
    },
    search: false,
  },
  {
    dataIndex: "jobDegree",
    title: "学历要求",
    valueType: "select",
    valueEnum: {
      学历不限: { text: "学历不限" },
      大专: { text: "大专" },
      本科: { text: "本科" },
      硕士: { text: "硕士" },
      博士: { text: "博士" },
    },
  },
  {
    dataIndex: "bossName",
    title: "联系人",
  },
  {
    dataIndex: "bossTitle",
    title: "联系人职位",
    search: false,
  },

  {
    title: "操作",
    valueType: "option",
    key: "option",
    render: (text, record, _, action) => [
      <Button type="primary" size="small">
        编辑
      </Button>,
      <Button type="primary" size="small" danger>
        删除
      </Button>,
      <TableDropdown
        key="actionGroup"
        onSelect={() => action?.reload()}
        menus={[
          { key: "copy", name: "复制" },
          { key: "delete", name: "删除" },
        ]}
      />,
    ],
  },
];

export default () => {
  const actionRef = useRef<ActionType>();
  const { userInfo } = useSelector((state: RootState) => state.user);
  return (
    <ProTable<JobType>
      columns={columns}
      actionRef={actionRef}
      cardBordered
      request={async (params = {}, sort, filter) => {
        console.log("查询参数", params);
        let res = await jobGet(userInfo!.objectId, params);
        return {
          data: res.data.results,
          success: true,
        };
      }}
      editable={{
        type: "multiple",
      }}
      columnsState={{
        persistenceKey: "pro-table-singe-demos",
        persistenceType: "localStorage",
        onChange(value) {
          console.log("value: ", value);
        },
      }}
      rowKey="id"
      search={{
        labelWidth: "auto",
      }}
      options={{
        setting: {
          listsHeight: 400,
        },
      }}
      form={{
        // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
        syncToUrl: (values, type) => {
          if (type === "get") {
            return {
              ...values,
              created_at: [values.startTime, values.endTime],
            };
          }
          return values;
        },
      }}
      pagination={{
        pageSize: 5,
        onChange: (page) => console.log(page),
      }}
      dateFormatter="string"
      headerTitle="高级表格"
      toolBarRender={() => [
        <Button
          key="button"
          icon={<PlusOutlined />}
          onClick={() => {
            actionRef.current?.reload();
          }}
          type="primary"
        >
          新建
        </Button>,
        <Dropdown
          key="menu"
          menu={{
            items: [
              {
                label: "1st item",
                key: "1",
              },
              {
                label: "2nd item",
                key: "1",
              },
              {
                label: "3rd item",
                key: "1",
              },
            ],
          }}
        >
          <Button>
            <EllipsisOutlined />
          </Button>
        </Dropdown>,
      ]}
    />
  );
};
